package com.manage;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.manage.base.BaseActivity;
import com.manage.sharedpreferences.SPConfig;
import com.manage.util.RequestUtils;
import com.manage.util.ToastUtils;

/**
 * 今日账目
 * @author liyoro
 * @date 2012-6-6
 * 
 */
public class AccountsTodayActivity extends BaseActivity implements OnClickListener {

	private static String TAG = "AccountsTodayActivity";

	private TextView tvTime = null;
	private TextView tvOrderCounts = null;
	private TextView tvAccepted = null;
	private TextView tvDelivery = null;
	private TextView tvAccounted = null;
	private TextView tvCancel = null;
	private TextView tvTurnover = null;
	private Button btnOrderDetails = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_manage_accounts_today);
		initDatas();
	}
	
	@Override
	public String getLeftBtnName() {
		return "返回";
	}

	@Override
	public String getTitleName() {
		return "今日账目";
	}

	@Override
	public boolean showButton() {
		return true;
	}

	@Override
	public void initView() {
		tvTime = (TextView)findViewById(R.id.tv_time);
		tvOrderCounts = (TextView)findViewById(R.id.tv_received);
		tvAccepted = (TextView)findViewById(R.id.tv_accepted);
		tvDelivery = (TextView)findViewById(R.id.tv_delivery);
		tvAccounted = (TextView)findViewById(R.id.tv_accounted);
		tvCancel = (TextView)findViewById(R.id.tv_cancel);
		tvTurnover = (TextView)findViewById(R.id.tv_turnover);
		btnOrderDetails =(Button)findViewById(R.id.btn_check_order);
		btnOrderDetails.setOnClickListener(this);
	}

	@Override
	public void initDatas() {
		String time;
	    //设置时间格式
		SimpleDateFormat format=new SimpleDateFormat( "yyyy年MM月dd日");
		time = format.format((new Date()));

		tvTime.setText(time);
		
		String token = sp.getString(SPConfig.ACCESSTOKEN, "0");
		String shopID = sp.getString(SPConfig.SHOPID, "");
//		Log.i(TAG, ":"+token+";"+shopID);
		RequestUtils.getData(token, shopID, "", "", ACCOUNTTODAYURL, handler, this.requestList);
	}
	
	public Handler handler = new Handler() {
		public void handleMessage(Message msg) { 
			switch(msg.what) {
			case SUCCESS:
				Bundle bundle = msg.getData();
				String res = bundle.getString("res");
				
				try {
					JSONObject temp = new JSONObject(res.substring(1, res.length() -1));
					Log.i(TAG, temp.toString());
					JSONObject temp2 = new JSONObject(temp.getString("return_info"));
					
					tvOrderCounts.setText(temp2.getString("total_orders"));
					tvAccepted.setText(temp2.getString("total_accepted_orders"));
					tvDelivery.setText(temp2.getString("total_delivery_orders"));
					tvAccounted.setText(temp2.getString("total_accounted_orders"));
					tvCancel.setText(temp2.getString("total_cancel_orders"));
					tvTurnover.setText(temp2.getString("turnover"));
					
				} 
				catch (JSONException e) {
					e.printStackTrace();
				}
				catch (Exception e) {
					e.printStackTrace();
				}
				break;
			case FAIL: //
				ToastUtils.show(AccountsTodayActivity.this, "获取数据失败，请稍侯重试！", true);
				break;
			}
		}
	};

	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.btn_check_order:
			Intent mIntent = new Intent();
			mIntent.setClass(AccountsTodayActivity.this, AccountsOrderDetailActivity.class);
			
			startActivity(mIntent);
			break;
		}
	}
}
